package com.mc.dao.Impl;

import com.mc.bean.Team;
import com.mc.dao.TeamDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lyx
 * 团队数据访问实现类
 */
public class TeamDaoImpl extends BaseDao implements TeamDao {
    @Override
    public List<Team> findAll() {
        List<Team> list = new ArrayList<>();
        try {
            //1加载驱动
            super.regist();
            //2创建连接
            super.connection();
            //3创建预编译指令
            String sql = "select teamid,teamname from team";
            super.getPst(sql);
            //4执行预编译指令，得到结果集
            rs = pstmt.executeQuery();
            while(rs.next()){
                Team team = new Team(rs.getInt(1),rs.getString(2));
                list.add(team);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5关闭
            super.close();
        }
        return list;
    }

    @Override
    public Team findTeamId(int teamid) {
        Team team = null;
        try {
            super.regist();
            super.connection();
            String sql = "select teamid,teamname from team where teamid = ?";
            super.getPst(sql);
            super.pstmt.setInt(1,teamid);
            rs = pstmt.executeQuery();
            while(rs.next()){
                team = new Team(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.close();
        }
        return team;
    }


    @Override
    public int add(Team team) {
        int i = 0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "insert into team (teamname) values (?)";
            super.getPst(sql);
            super.pstmt.setString(1,team.getTeamname());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int delete(int teamid) {
        int i = 0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "delete from team where teamid = ?";
            super.getPst(sql);
            super.pstmt.setInt(1,teamid);
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int update(Team team) {
        int i = 0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "update team set teamname = ? where teamid = ?";
            super.getPst(sql);
            super.pstmt.setString(1,team.getTeamname());
            super.pstmt.setInt(2,team.getTeamid());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }
}
